<template>
    <div>
        <el-row>
            <el-col :span="24">
                <h1>电影管理</h1>
            </el-col>
        </el-row>
        <el-row v-if="!updateShow">
            <el-col>
                <el-button @click="handleList">刷新</el-button>
                <el-button @click="handleInsert">添加电影类型</el-button>
            </el-col>
            <el-col>
                <el-input placeholder="请输入邮箱或手机号或用户名查询" v-model="input" class="input-with-select">
                    <el-button slot="append" icon="el-icon-search" @click="handlePage"></el-button>
                </el-input>
            </el-col>
            <el-col :span="24">
                <el-table
                        v-loading="loading"
                        :data="tableData"
                        style="width: 100%"
                        @sort-change="sortTable">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="movieTypeName"
                            label="类型名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="sort"
                            label="排序"
                            width="180"
                            sortable>
                    </el-table-column>
                    <el-table-column
                            prop="movieNum"
                            label="电影数量"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="movieTypeDescribe"
                            label="类型描述"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="insertTime"
                            label="添加时间"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="updateTime"
                            label="修改时间"
                            width="180">
                    </el-table-column>
                    <el-table-column  label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="handleUpdate(scope.$index)">修改</el-button>
                            <el-popconfirm
                                    confirm-button-text='好的'
                                    cancel-button-text='不用了'
                                    icon="el-icon-info"
                                    icon-color="red"
                                    title="这是一段内容确定删除吗？"
                                    @confirm="handleDelete(scope.$index)"
                            >
                                <el-button slot="reference" size="mini">删除</el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        <el-form v-if="updateShow" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="类型名" prop="movieTypeName">
                <el-input v-model="ruleForm.movieTypeName"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item label="类型描述" prop="describe">
                <el-input v-model="ruleForm.movieTypeDescribe"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
                <el-button @click="handleInsert">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {

        name: "MovieTypeListView.vue",
        data() {
            return {
                loading: true,
                tableData: [],
                tableDateCopy: [],
                actor: {},
                updateShow: false,
                input: '',
                ruleForm: {
                    movieTypeName: '',
                    sort: '',
                    movieTypeDescribe: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入人员名称', trigger: 'blur' },
                    ],
                    sort: [
                        { required: true, message: '请输入默认排序', trigger: 'blur' },
                    ],
                    movieTypeDescribe: [
                        { required: true, message: '请输入类型描述', trigger: 'blur' }
                    ]
                },
            }
        },
        created() {
            this.handleList();
        },
        methods:{
            handleList(){
                this.axios.get("manage/movieType/list").then(response=> {
                    if (response.data.code == 200) {
                        this.tableData = response.data.data;
                        this.tableDateCopy=[...this.tableData]
                    }
                }).finally(()=>this.loading=false)
            },
            handleUpdate(index){
                const data=this.tableData[index];
                this.ruleForm=data;
                this.updateShow=!this.updateShow;
            },
            handleInsert(){
                this.ruleForm={};
                this.updateShow=!this.updateShow;
            },
            handleDelete(index){
                this.axios.get("manage/movieType/delete",{params:{id:this.tableData[index].id}}).then(response=>{
                    if (response.data.code==200){
                        this.tableData.splice(index,1);
                    }
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.axios.post("manage/movieType/saveOrUpdate",this.ruleForm,{params:{}}).then(response=>{
                            this.updateShow=false;
                            this.ruleForm={};
                        })
                    } else {
                        console.log('错误填写');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handlePage(){
                this.axios.get("manage/movieType/list",{params:{keyword:this.input}}).then(response=>{
                    if (response.data.code){
                        this.tableData = response.data.data;
                    }
                })
            },
            sortTable({prop, order}) {
                if (order === "ascending") {
                    this.tableData.sort((a, b) => this.sortName(a[prop], b[prop]));
                } else if (order === "descending") {
                    this.tableData.sort((a, b) => this.sortName(b[prop], a[prop]));
                } else {
                    this.tableData = [...this.tableDateCopy];
                }
            },
            sortName(a, b) {
                if (a < b) return -1;
                if (a > b) return 1;
                return 0;
            },
        }
    }
</script>

<style scoped>

</style>